$(function () {
    var grid;
//    setTimeout(function () {
//        $('#bgjhlrForm').tabs('select', "保供物资计划明细");
//    }, 100);

    //获取产地
//    var cdOptions = [];
    $.post($.webPath() + '/base/sjzdsy/get_sjzdz', {'bm': 'cd'}, function (data) {
        var cdOptions = data;
        bgjhmx(cdOptions);
//        grid = $('#bgjhlr_bgjhmx_edatagrid').edatagrid(options);
    });


    //新增物资计划明细
    $("#bgjhlr_bgjhmx_toolbar [name='xzwzmx']").click(function () {
        searchcodewindow(openSearchWindowOKbtnHandle, 1);
    });
//    //新增物资计划明细
//    $("#bgjhlr_bgjhmx_toolbar [name='xzyh']").click(function () {
//        var obj = {id: "", 'wzgg.id': "", 'wzgg.oldcode': '', 'wzgg.code': '', 'wzgg.name': '', 'wzgg.ggxh': '', 'th': '', 'sbcl': '', 'dw': '', 'cd.id': null, 'jhjs': 0, 'jhsl': 0, 'dhrq': '', 'cz': '', 'bz': ''};
//        $(grid).edatagrid('appendRow', obj);
//    });
//    //新增物资计划明细
//    $("#bgjhlr_bgjhmx_toolbar [name='xzwh']").click(function () {
//
//        for (var i = 0; i < 5; i++) {
//            var obj = {id: "", 'wzgg.id': "", 'wzgg.oldcode': '', 'wzgg.code': '', 'wzgg.name': '', 'wzgg.ggxh': '', 'th': '', 'sbcl': '', 'dw': '', 'cd.id': null, 'jhjs': 0, 'jhsl': 0, 'dhrq': '', 'cz': '', 'bz': ''};
//            $(grid).edatagrid('appendRow', obj);
//        }
//
//    });
//    //新增物资计划明细
//    $("#bgjhlr_bgjhmx_toolbar [name='xzsh']").click(function () {
//        for (var i = 0; i < 10; i++) {
//            var obj = {id: "", 'wzgg.id': "", 'wzgg.oldcode': '', 'wzgg.code': '', 'wzgg.name': '', 'wzgg.ggxh': '', 'th': '', 'sbcl': '', 'dw': '', 'cd.id': null, 'jhjs': 0, 'jhsl': 0, 'dhrq': '', 'cz': '', 'bz': ''};
//            $(grid).edatagrid('appendRow', obj);
//        }
//
//    });

    //删除物资计划明细
    $("#bgjhlr_bgjhmx_toolbar [name='scwzmx']").click(function () {
        var rows = $(grid).edatagrid('getChecked');
        if (rows.length > 0) {
            $.messager.confirm('提示', '是否确定删除物资明细?', function (r) {
                if (r) {
                    $.each(rows, function (i, val) {
                        if (val.id) {
                            $.post($.webPath() + "/jhgl/bgjhlr/delMx", {'bgjhmxId': val.id}, function (data) {
                                var index = $(grid).edatagrid('getRowIndex', val);
                                $(grid).edatagrid('deleteRow', index);
                            })
                        } else {
                            var index = $(grid).edatagrid('getRowIndex', val);
                            $(grid).edatagrid('deleteRow', index);
                        }

                    });
                }
            });
        } else {
            $.messager.alert("提示", "请勾选要删除的物资明细!");
        }
        $(grid).edatagrid('resize');
    });
    //保供计划明细退回
    $("#bgjhlr_bgjhmx_toolbar [name='th']").click(function () {
        var rows = $(grid).edatagrid('getChecked');
        if (rows.length > 0) {
            $.messager.confirm('提示', '是否确定退回物资明细?', function (r) {
                if (r) {
                    $.mask();
                    $.each(rows, function (i, val) {
                        if (val.id) {
                            $.post($.webPath() + "/jhgl/bgjhlr/mxth", {'bgjhmxId': val.id}, function (data) {
                                $.mask.hide();
                                var obj = eval('(' + data + ')');
                                if (obj.statusCode == 200) {
                                    $(grid).edatagrid("reload");
                                } else {
                                    $.messager.alert("提示", obj.message, "warning");
                                }
                            })
                        } else {
                            $.mask.hide();
                        }
                    });
                }
            });
        } else {
            $.messager.alert("提示", "请勾选要删除的物资明细!");
        }
        $(grid).edatagrid('resize');
    });
    //查询物资代码确定按钮回调函数
    function openSearchWindowOKbtnHandle(data) {
//        var grid;
        var dqrq = $("#bgjhlr_content_dqrq").val();
        var rowdatas = $.map(data, function (n, i) {
            var temp = {
                'id': '',
                'wzgg.id': n.id,
                'wzgg.oldcode': n.code,
                'wzgg.code': n.code,
                'wzgg.name': n.name,
                'wzgg.ggxh': n.ggxh,
                'dw': n.dw,
                'ph': n.ph,
                'wzgg.cz': n.cz,
                'cd.id': null,
                'sbcl': 'cl',
                'jhjs': 0,
                'jhsl': 0,
                'dhrq': dqrq,
                'cz': n.cz,
                'th': n.th,
                'bz': ''
            };
            return temp;
        });
        $.each(rowdatas, function (i, obj) {
            var rows = $(grid).edatagrid('getRows');
//            var flag = false;
            $.each(rows, function (i, objs) {
                if (!objs['wzgg.code']) {
                    $(grid).edatagrid('deleteRow', i);
                }
//                else if (obj['wzgg.code'] == objs['wzgg.code']) {
//                    flag = true;
//                    return false;
//                }
            })
//            if (!flag) {
            $(grid).edatagrid('appendRow', obj);
//            }
//            var obj = {id: "", 'wzgg.id': "", 'wzgg.oldcode': '', 'wzgg.code': '', 'wzgg.name': '', 'wzgg.ggxh': '', 'th': '', 'sbcl': '', 'dw': '', 'cd.id': '', 'jhjs': 0, 'jhsl': 0, 'dhrq': '', 'cz': '', 'bz': ''};
//            $(grid).edatagrid('appendRow', obj);
        });
    }

    //表格处在编辑状态的时候，给表单绑定事件
//    function datagridOnSelectHandle(rowIndex, rowData) {
//        //获取每个单元格
//        $(grid).edatagrid('editRow', rowIndex);
//        var editors = $(grid).datagrid('getEditors', rowIndex);
//        if (editors.length == 0)
//            return;
//        var bm1Editor = editors[0]; // 物资编码编辑框
//
//        if ($("#bgjhlr_content_bgjh_id").val() && rowData['wzgg.id']) {
//            $(bm1Editor.target).attr("readonly", "true");
//        } else {
//            $(bm1Editor.target).removeAttr("readonly");
//        }
////        var bmEditor = editors[1]; // 物资编码编辑框
////        $(bmEditor.target).attr("readonly", "true");
//        var mcEditor = editors[1];
//        $(mcEditor.target).attr("readonly", "true");
//        var ggEditor = editors[2]; //规格编辑框
//        $(ggEditor.target).attr("readonly", "true");
//       // var thEditor=editors[3];//图号编辑框
//      //  $(thEditor.target).attr("readonly", "true");
//        var cdEditor = editors[5];// 产地编辑框
//
//
//        var sbcl = editors[3];
//        var dw = editors[4];
////        var ph = editors[6];
//        var js = editors[6];
//        var sl = editors[7];
//        var dhrq = editors[8];
//        var cz = editors[9];
//       // var th = editors[3];
//        var bz = editors[10];
//        $(cdEditor.target).combobox({
//            onLoadSuccess:function(){
//                if (rowData['cd.id'])
//                    $(cdEditor.target).combobox('setValue', rowData['cd.id']);
//            },
//            onSelect: function (record) {
//                var jscount = $(js.target).numberbox('getValue');
//                if (jscount && jscount != '0') {
//                    var selected = $('#bgjhlr_bgjhmx_edatagrid').datagrid('getSelected');
//
//                    $.post($.webPath() + '/cggl/cgdgl/get_jz', {'ggId': selected['wzgg.id'], cdId: record.id}, function (data) {
//                        var obj = eval('(' + data + ')');
//                        if (obj.jz && obj.jz != '') {
//                            $(sl.target).numberbox('setValue', parseFloat(obj.jz) * $(js.target).numberbox('getValue'));
//                        } else {
//                            $(sl.target).numberbox('setValue', 0);
//                            // $.messager.alert('提示', '此品牌物资未存单位件重，请手动填写申报数量', 'info');
//                        }
//                    })
//                }
//            }
//        });
//
//        $(js.target).unbind('blur').bind('blur', function () {
//            $(js.target).numberbox('setValue', $(js.target).val());
//            var cdVal = $(cdEditor.target).combobox('getValue');
//            if (cdVal != "" && cdVal) {
//                var selected = $('#bgjhlr_bgjhmx_edatagrid').datagrid('getSelected');
//                $.post($.webPath() + '/cggl/cgdgl/get_jz', {'ggId': selected['wzgg.id'], cdId: $(cdEditor.target).combobox('getValue')}, function (data) {
//                    var obj = eval('(' + data + ')');
//                    if (obj.jz && obj.jz != '') {
//                        $(sl.target).numberbox('setValue', parseFloat(obj.jz) * $(js.target).numberbox('getValue'));
//                    } else {
//                        $(sl.target).numberbox('setValue', 0);
////                        $.messager.alert('提示', '此物资未存单位件重，请手动填写申报数量', 'info');
//                        $(sl.target).select();
//                    }
//                })
//            }
//        })
//
//        $(bm1Editor.target).unbind('keyup').bind('keyup', function (event) {
//            var code = $(bm1Editor.target).val();
//            if (event.keyCode == "13") {
////                $.post($.webPath() + '/jhgl/bgjhlr/checkWzmx', {'id':$("#bgjhlr_content_bgjh_id").val(), wzbm:$(bm1Editor.target).val()}, function (data) {
////                    if (data.statusCode == '200') {
//                $.post($.webPath() + '/sys/wzbm/search', {code: $(bm1Editor.target).val(), 'page': 1, 'rows': -1}, function (data) {
//                    var rows = $("#bgjhlr_bgjhmx_edatagrid").datagrid('getData').rows;
//                    var num = 0;
//                    for (var i = 0; i < rows.length; i++) {
//                        if (rows[i]['wzgg.code'] == code) {
//                            num++;
//                        }
//                    }
//                    if (num > 0) {
//                        $.messager.alert("提示", "您填写的物资编码已重复", "info", function () {
//                            setTimeout(function () {
//                                $(bm1Editor.target).select();
//                            }, 500);
//
//                            return false;
//                        });
//                    } else {
//                        num = 0;
//                        if (data.rows.length == 1) {
//                            var dqrq = $("#bgjhlr_content_dqrq").val();
//                            var row = data.rows[0];
////                        $(bmEditor.target).val(row.code);
//                            $(mcEditor.target).val(row.name);
//                            $(ggEditor.target).val(row.ggxh);
//                            $(cdEditor.target).combobox().next('span').find('input').focus();
//
////                            $(js.target).select();
//                            $(dw.target).val(row.dw);
//                            $(cz.target).val(row.cz);
//                            $(th.target).val(row.th);
////                        $(cdEditor.target).combobox('setValue', 17265);
//                            if (rowIndex >= 1) {
//                                var griddata = $(grid).edatagrid('getData');
//                                var rows = griddata.rows;
//                                if (rows[rowIndex - 1].sbcl) {
//                                    $(sbcl.target).combobox('setValue', rows[rowIndex - 1].sbcl);
//                                } else {
//                                    $(sbcl.target).combobox('setValue', 'cl');
//                                }
//                                if (rows[rowIndex - 1]['cd.id']) {
//                                    $(cdEditor.target).combobox('setValue', rows[rowIndex - 1]['cd.id']);
//                                }
////                                else {
////                                    $(cdEditor.target).combobox('setValue', 'cl');
////                                }
//                                $(dhrq.target).datebox('setValue', rows[rowIndex - 1].dhrq);
//                                $(bz.target).val(rows[rowIndex - 1].bz);
//                            } else {
//                                $(sbcl.target).combobox('setValue', 'cl');
//                                $(dhrq.target).datebox('setValue', dqrq);
//                                $(bz.target).val('');
//                            }
//
//                            rowData['wzgg.id'] = row.id;
//                            var obj = {id: "", 'wzgg.id': "", 'wzgg.oldcode': '', 'wzgg.code': '', 'wzgg.name': '', 'wzgg.ggxh': '', 'th': '', 'sbcl': '', 'dw': '', 'cd.id': '', 'jhjs': 0, 'jhsl': 0, 'dhrq': '', 'cz': '', 'bz': ''};
//                            $(grid).edatagrid('appendRow', obj);
//                        } else if (data.rows.length > 1) {
//                            for (var i = 0; i < data.rows.length; i++) {
//                                var dqrq = $("#bgjhlr_content_dqrq").val();
//                                var row = data.rows[i];
//                                if ($(bm1Editor.target).val() == row.code) {
//                                    $(mcEditor.target).val(row.name);
//                                    $(ggEditor.target).val(row.ggxh);
//
//                                    $(cdEditor.target).combobox().next('span').find('input').focus()
////                                    $(js.target).select();
//                                    $(dw.target).val(row.dw);
//                                    $(cz.target).val(row.cz);
//                                    $(th.target).val(row.th);
//                                    if (rowIndex >= 1) {
//                                        var griddata = $(grid).edatagrid('getData');
//                                        var rows = griddata.rows;
//                                        if (rows[rowIndex - 1].sbcl) {
//                                            $(sbcl.target).combobox('setValue', rows[rowIndex - 1].sbcl);
//                                        } else {
//                                            $(sbcl.target).combobox('setValue', 'cl');
//                                        }
//                                        if (rows[rowIndex - 1]['cd.id']) {
//                                            $(cdEditor.target).combobox('setValue', rows[rowIndex - 1]['cd.id']);
//                                        }
//                                        $(dhrq.target).datebox('setValue', rows[rowIndex - 1].dhrq);
//                                        $(bz.target).val(rows[rowIndex - 1].bz);
//                                    } else {
//                                        $(sbcl.target).combobox('setValue', 'cl');
//                                        $(dhrq.target).datebox('setValue', dqrq);
//                                        $(bz.target).val('');
//                                    }
//
//                                    rowData['wzgg.id'] = row.id;
//                                    var obj = {id: "", 'wzgg.id': "", 'wzgg.oldcode': '', 'wzgg.code': '', 'wzgg.name': '', 'wzgg.ggxh': '', 'th': '', 'sbcl': '', 'dw': '', 'cd.id': '', 'jhjs': 0, 'jhsl': 0, 'dhrq': '', 'cz': '', 'bz': ''};
//                                    $(grid).edatagrid('appendRow', obj);
//                                    return false;
//                                }
//                            }
//                            $.messager.alert("提示", "输入的物资编码无物资", "info", function () {
//                                setTimeout(function () {
//                                    $(bm1Editor.target).select();
//                                }, 500);
//
//                                return false;
//                            });
//                        } else {
//                            $.messager.alert("提示", "输入的物资编码无物资", "info", function () {
//                                setTimeout(function () {
//                                    $(bm1Editor.target).select();
//                                }, 500);
//
//                                return false;
//                            });
//                        }
//                    }
//                }, 'json');
////                    } else {
////                        $.messager.alert("提示", data.message);
////                    }
////                }, 'json');
//
//            }
//        })
//
//        $(bz.target).unbind('blur').bind('blur', function () {
//            $(grid).edatagrid('selectRow', rowIndex + 1);
//        })
//
//        $(bm1Editor.target).focus();
//    }


    function bgjhmx(cdOptions) {
        //===========================
//========物资计划明细===========
//===========================
        var columns = [
            [
                {field: 'checked', checkbox: true},
                {field: 'id', title: 'ID', hidden: true},
                {field: 'wzgg.id', title: '物资编码ID', hidden: true},
                {title: '物资编码', field: 'wzgg.code', align: 'center', width: 170, hidden: true},
//            {title: '物资编码(新)', field: 'wzgg.code', editor: {type: 'text'}, width: 150},
                {title: '物资名称', field: 'wzgg.name', align: 'center', width: 150},
                {title: '规格', field: 'wzgg.ggxh', align: 'center', width: 200},
                {title: '材质', field: 'wzgg.cz', align: 'center', width: 150},
                //{title: '图号', field: 'wzgg.th', align: 'center', editor: {type: 'text'}, width: 100},
                {title: '设备材料', field: 'sbcl', align: 'center', editor: {type: 'combobox', options: {valueField: 'sbcl', textField: 'text', panelHeight: '60', data: [
                    {
                        sbcl: 'cl',
                        text: '材料'
                    },
                    {
                        sbcl: 'sb',
                        text: '设备'
                    }
                ]}}, formatter: function (value, row, index) {
                    if (value == 'cl') {
                        return '材料';
                    } else if (value == 'sb') {
                        return '设备';
                    }
                }, width: 150
                },
                {title: '单位', field: 'dw', align: 'center', width: 50},
                {title: '产地', field: 'cd.id', align: 'center', editor: {type: 'combobox', options: {
                    data: cdOptions,
                    valueField: 'id',
                    textField: 'z',
                    panelHeight: '100',
                    width: '80'
                }}, formatter: function (value) {
                    for (var i = 0; i < cdOptions.length; i++) {
                        if (cdOptions[i].id == value) return cdOptions[i].z;
                    }
                    return value;
                }, width: 150},
                {title: '计划件数', field: 'jhjs', align: 'right', editor: {type: 'numberbox', options: {required: true, min: 0, groupSeparator: ',', precision: 0}},
                    formatter: function (value) {
                        var sl = formatSlForHtml(value, ",");
                        return sl;
                    }, width: 150},
                {title: '计划重量', field: 'jhsl', align: 'right', editor: {type: 'numberbox', options: {required: true, min: 0, groupSeparator: ',', precision: 4}},
                    formatter: function (value) {
                        var sl = formatSlForHtml(value, ",");
                        return sl;
                    }, width: 150},
                {title: '到货日期', field: 'dhrq', align: 'center', editor: {type: 'datebox'}, width: 150},
                {title: '备注', field: 'bz', align: 'center', editor: {type: 'text'}, width: 150}
            ]
        ];

        var options = {
            toolbar: '#bgjhlr_bgjhmx_toolbar',
            fitColumns: true,
            columns: columns,
            fit: true,
            checkbox: true,
            checkOnSelect: false,
            selectOnCheck: false,
            singleSelect: true,
            rownumbers: true,
//            onClickRow: function (rowIndex, rowData) {
//                datagridOnSelectHandle(rowIndex, rowData);
//            },
            onSelect: function (rowIndex, rowData) {
//                datagridOnSelectHandle(rowIndex, rowData);
            },
            onLoadSuccess: function () {
                var obj = {id: "", 'wzgg.id': "", 'wzgg.oldcode': '', 'wzgg.code': '', 'wzgg.name': '', 'wzgg.ggxh': '', 'sbcl': '', 'dw': '', 'cd.id': null, 'jhjs': 0, 'jhsl': 0, 'dhrq': '', 'cz': '', 'th': '', 'bz': ''};
                $(grid).edatagrid('appendRow', obj);

                $(".datagrid-htable .datagrid-cell").css('text-align', 'center');
            },
            onAfterEdit: function (rowIndex, rowData, changes) {

                if (!rowData.id)
                    rowData.id = '';
                if (rowData['wzgg.id']) {
                    if (rowData['cd.id']) {
                        var paramString = "{\"bgjhmx.id\":\"" + rowData.id + "\",\"bgjhmx.wzgg.id\":\"" + rowData['wzgg.id'] + "\",\"bgjhmx.sbcl\":\"" + rowData['sbcl'] + "\",\"bgjhmx.dw\":\"" + rowData['dw'] + "\",\"bgjhmx.jhjs\":\"" + rowData['jhjs'] + "\",\"bgjhmx.jhsl\":\"" + rowData['jhsl'] + "\",\"bgjhmx.dhrq\":\"" + rowData['dhrq'] + "\",\"bgjhmx.bz\":\"" + rowData['bz'] + "\",\"bgjhmx.cd.id\":\"" + rowData['cd.id'] + "\",\"bgjhmx.bgjh.id\":\"" + $("#bgjhlr_content_bgjh_id").val() + "\"}";
                        $.post($.webPath() + "/jhgl/bgjhlr/saveMx", $.parseJSON(paramString), function (data) {
                            rowData.id = data.mxid;
                        }, 'json')
                    } else {
                        $.messager.alert("提示", "产地不能为空!", "waring");
                    }
                }
            }
        };

        if ($("#bgjhlr_content_bgjh_id").val() != "") {
            $.extend(options, {url: $.webPath() + '/jhgl/bgjhlr/bgjhmxList?id=' + $("#bgjhlr_content_bgjh_id").val()});
        } else {
            $.extend(options, {data: {
                rows: [
                    {id: "", 'wzgg.id': "", 'wzgg.oldcode': '', 'wzgg.code': '', 'wzgg.name': '', 'wzgg.ggxh': '', 'th': '', 'sbcl': '', 'dw': '', 'cd.id': null, 'jhjs': 0, 'jhsl': 0, 'dhrq': '', 'cz': '', 'bz': ''},
                    {id: "", 'wzgg.id': "", 'wzgg.oldcode': '', 'wzgg.code': '', 'wzgg.name': '', 'wzgg.ggxh': '', 'th': '', 'sbcl': '', 'dw': '', 'cd.id': null, 'jhjs': 0, 'jhsl': 0, 'dhrq': '', 'cz': '', 'bz': ''},
                    {id: "", 'wzgg.id': "", 'wzgg.oldcode': '', 'wzgg.code': '', 'wzgg.name': '', 'wzgg.ggxh': '', 'th': '', 'sbcl': '', 'dw': '', 'cd.id': null, 'jhjs': 0, 'jhsl': 0, 'dhrq': '', 'cz': '', 'bz': ''},
                    {id: "", 'wzgg.id': "", 'wzgg.oldcode': '', 'wzgg.code': '', 'wzgg.name': '', 'wzgg.ggxh': '', 'th': '', 'sbcl': '', 'dw': '', 'cd.id': null, 'jhjs': 0, 'jhsl': 0, 'dhrq': '', 'cz': '', 'bz': ''},
                    {id: "", 'wzgg.id': "", 'wzgg.oldcode': '', 'wzgg.code': '', 'wzgg.name': '', 'wzgg.ggxh': '', 'th': '', 'sbcl': '', 'dw': '', 'cd.id': null, 'jhjs': 0, 'jhsl': 0, 'dhrq': '', 'cz': '', 'bz': ''}
                ]
            }
            })
        }

        grid = $('#bgjhlr_bgjhmx_edatagrid').edatagrid(options);
    }
})

